#include "Player.h"
#include "CPlayerGUI.h"
#include "SpellManager.h"

/*
#include "Spell.h"
#include "SpellStat.h"
*/

#include "Input.h"
#include "Level.h"

Player::Player(void)
{
	rotation.set(0,0,0);
	radiusxz = 10;
	height = 50;
	speed.set(0,0,0);
}

Player::~Player(void)
{
}

void Player::isCasting(float dt)
{
	// PLAYER IS NOT CASTING, CASTING TIME RESETS ////////////
	if (dt == 0)
	{
		CPlayerGUI::m_dCastingTime = 0;
		return;
	}
	//////////////////////////////////////////////////////////
	
	// PLAYER JUST STARTED CASTING, GETTING TOTAL CAST TIME //
	if ((CPlayerGUI::m_dCastingTime == 0) && (CPlayerGUI::m_dCooldown[CPlayerGUI::m_eCurrentSpell] > 0))
		CPlayerGUI::m_dTotalCastingTime = SpellStat::CastingTime[CPlayerGUI::m_eCurrentSpell];
	//////////////////////////////////////////////////////////
	

	// ADDING DT TO CAST TIME ////////////////////////////////
	CPlayerGUI::m_dCastingTime+=dt;
	//////////////////////////////////////////////////////////

	// SPELL IS CAST! ////////////////////////////////////////
	if (CPlayerGUI::m_dCastingTime >= CPlayerGUI::m_dTotalCastingTime)
	{
		//cout << "Level::currentLevel->camera->getTarget().x: " << rotation.X << endl << "Level::currentLevel->camera->getTarget().y: " << Level::currentLevel->camera->getTarget().Y << endl << "Level::currentLevel->camera->getTarget().z: " << Level::currentLevel->camera->getTarget().Z << endl;
		SpellManager::CastSpell(*this, CPlayerGUI::m_eCurrentSpell, Level::currentLevel->camera->getTarget() );
		CPlayerGUI::m_dCastingTime = 0;		
	}
	//////////////////////////////////////////////////////////
	
	// que es este sonido? ---> lo mande a SpellManager::CastSpell()
	//ResourceManager::PlaySound(ResourceManager::SOUND::Ball);
}

void Player::Update(float delta_time)
{
	static float h;
	// con esto corrijo la gravedad de la camara....hago que caiga con el jugador.
	h = position.Y;
	position = Level::currentLevel->camera->getPosition();
	position.Y = h;
	//Level::currentLevel->camera->setPosition( position );
	
	rotation = Level::currentLevel->camera->getRotation();
	
	// cout << "rotation.x: " << rotation.X << "rotation.y: " << rotation.Y << "rotation.z: " << rotation.Z << endl;
	if(Input::MLeft)
		isCasting(delta_time);
	else isCasting(0);
	
	/* a la mierda con todo esto!!!
	if(Input::keyPressed(GK_UP))
		speed.X = SPEED_MOVE;
	
	if(Input::keyPressed(GK_DOWN))
		speed.X = -SPEED_MOVE;
	
	if(Input::keyPressed(GK_LEFT))
		speed.Z = -SPEED_MOVE;
	
	if(Input::keyPressed(GK_RIGHT))
		speed.Z = SPEED_MOVE;	
	
	if(Input::keyPressed(GK_CROUCH))
	{}
	*/
	
	if(Input::keyPressed(GK_JUMP))
	{
		if (!Falling)
			speed.Y = SPEED_JUMP;
	}
}

void Player::Destroy()
{
}

void Player::onCollide(Entity* e)	
{
	//cout << "el player colisiono contra una Entidad" <<endl;
}

void Player::onCollide()
{
	//cout << "El player esta colisionando contra el piso!" << endl;
}